package com.pur.zsms.documentsmanage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.CipherUtil;
import com.pur.zsms.common.util.DeleteFileUtil;
import com.pur.zsms.documentsmanage.dao.IDomDao;
import com.pur.zsms.documentsmanage.po.DomPo;
import com.pur.zsms.documentsmanage.service.IDomService;
import com.pur.zsms.documentsmanage.vo.DomVo;
import com.pur.zsms.documentsmanage.web.DomControllor;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.systemsetting.datadictionary.po.Code;

@Service("domService")
public class DomServiceImpl implements IDomService {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(DomServiceImpl.class);

	@Resource(name = "domDao")
	private IDomDao domDaoImpl;

	public IDomDao getDomDaoImpl() {
		return domDaoImpl;
	}

	public void setDomDaoImpl(IDomDao domDaoImpl) {
		this.domDaoImpl = domDaoImpl;
	}

	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getDomMenu(int domId) throws GenericException {

		StringBuffer jsonString = new StringBuffer("");
		List<DomPo> listDomPo = domDaoImpl.getDomPoListByParentId(domId);
		if (listDomPo != null && listDomPo.size() > 0) {
			jsonString.append("[");
			for (DomPo domPo : listDomPo) {
				jsonString.append("{\"");
				jsonString.append("id");
				jsonString.append("\":\"");
				jsonString.append(domPo.getDomId());
				jsonString.append("\",\"text\":\"");
				jsonString.append(domPo.getDomText());
				jsonString.append("\"");

				if (domPo.getIsFolder() == 1) {
					jsonString.append(",\"state\":\"closed\"");
					jsonString.append(",\"folder\":\"1\"");
				}

				if (domPo.getDomLink() != null && !domPo.getDomLink().equals("")) {
					jsonString.append(",\"link\":\"");
					jsonString.append("1");
					jsonString.append("\"");
				}
				jsonString.append("},");
			}
			jsonString = new StringBuffer(jsonString.substring(0, jsonString.length() - 1));
			jsonString.append("]");
			return jsonString.toString();
		} else {
			return "[]";
		}
	}

	public String getLinkById(int domId) throws GenericException {
		return domDaoImpl.getLinkById(domId).get(0).getDomLink();
	}

	public DomPo getDomPoById(int domId) throws GenericException {
		return domDaoImpl.getLinkById(domId).get(0);
	}

	public boolean addDomPo(DomVo domVo) throws GenericException {
		// 检查同名
		if (checkFolder(domVo)) {
			return false;
		}
		domVo.setIsFolder(1);
		DomPo domPo = new DomPo();
		BeanCopier copier = BeanCopier.create(DomVo.class, DomPo.class, false);
		copier.copy(domVo, domPo, null);
		domDaoImpl.addDomPo(domPo);

		return true;

	}

	public boolean checkFolder(DomVo domVo) throws GenericException {
		// 检查同名
		List<DomPo> list = domDaoImpl.getDomPoByText(domVo.getDomText());
		if (list != null && list.size() > 0) {
			this.msg = "msg001";
			return true;
		}
		return false;
	}

	public boolean checkFolderParentForSup(DomVo domVo) throws GenericException {
		List<DomPo> list = domDaoImpl.getDomPoByText(domVo.getDomText());
		if (list != null && list.size() > 0) {
			this.msg = "msg001";
			return true;
		}
		return false;
	}

	public void addDomPoForContext(DomVo domVo) throws GenericException {
		domVo.setIsFolder(0);
		DomPo domPo = new DomPo();
		BeanCopier copier = BeanCopier.create(DomVo.class, DomPo.class, false);
		copier.copy(domVo, domPo, null);
		log.info("----zzy332--" + domVo.getDomLink());

		log.info("----zzy333--" + domPo.getDomLink());

		domDaoImpl.addDomPo(domPo);

	}

	public boolean deleteDomPo(DomVo domVo) throws GenericException {
		if (domVo.getDomId() == 1) {
			msg = "msg002";
			return false;
		}
		deleteFolder(domVo.getDomId());
		return true;
	}

	public void deleteFolder(int domId) throws GenericException {

		String link = domDaoImpl.getDomPoByDomId(domId).get(0).getDomLink();
		if (link != null && !link.equals("")) {
			DeleteFileUtil.deleteFile(link);
		} else {
			List<DomPo> list = domDaoImpl.getDomPoListByParentId(domId);
			if (list != null && list.size() > 0) {
				for (DomPo domPo : list) {
					deleteFolder(domPo.getDomId());
				}
			}
		}
		domDaoImpl.deleteDomPo(domId);
	}
}
